var elLaunch = {
    observerService: null,

    init: function()
    {
        elLaunch.observerService = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
        elLaunch.observerService.addObserver(elLaunch, "ellaunch-launch-request", false);
    },
    complete: function ()
    {
        elLaunch.observerService.removeObserver(elLaunch, "ellaunch-launch-request");
    },
    observe: function(subject, topic, data)
    {
        if (topic == "ellaunch-launch-request") {
            var url_raw = data;
            if ((pos = url_raw.indexOf("://")) == -1) return;

            var url_scheme = url_raw.substring(0, pos);
            var url_server = url_raw.substring(pos + 3);
            url_server = url_server.replace("+connect/", "");
            url_server = url_server.replace("/", "");
            var url_ip = '';
            var url_port = '';
            if ((pos = url_server.lastIndexOf(":")) == -1) {
                url_ip = url_server;
            } else {
                url_ip = url_server.substring(0, pos);
                url_port = url_server.substring(pos + 1);
            }

            var name = null;
            var path = null;
            var args = null;
            var prefService = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
            try {
                name = prefService.getComplexValue('extensions.ellaunch.game_' + url_scheme + '_name', Components.interfaces.nsISupportsString).data;
            } catch (e) {
                return;
            }
            try {
                path = prefService.getComplexValue('extensions.ellaunch.game_' + url_scheme + '_path', Components.interfaces.nsISupportsString).data;
            } catch (e) {
                path = '';
            }
            try {
                args = prefService.getComplexValue('extensions.ellaunch.game_' + url_scheme + '_args', Components.interfaces.nsISupportsString).data;
            } catch (e) {
                args = '';
            }

            args = args.replace('%IP%', url_ip);
            args = args.replace('%PORT%', url_port);

            if (!window.confirm(name + "のサーバ (" + url_server + ")に接続しようとしています。続行しますか？")) return;

            if (path.length == 0) {
                var result = elLaunch.browseFile(name);
                if (result === false || result.length == 0) return;

                path = result;

                // save to preference
                var pref_path = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
                pref_path.data = path;
                prefService.setComplexValue('extensions.ellaunch.game_' + url_scheme + '_path', Components.interfaces.nsISupportsString, pref_path);
            }

            // Launch!
/* 
            //カレントディレクトリ設定出来ないって…
            var file = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
            file.initWithPath(path);
            var process = Components.classes["@mozilla.org/process/util;1"].createInstance(Components.interfaces.nsIProcess);
            process.init(file);
            var process_args = [game_connect_args, args];
            process.run(false, process_args, process_args.length);
*/

            var unicodeConverter = Components.classes["@mozilla.org/intl/scriptableunicodeconverter"].getService(Components.interfaces.nsIScriptableUnicodeConverter);
            unicodeConverter.charset = "Shift_JIS";
            path = unicodeConverter.ConvertFromUnicode(path);

            var work_dir = null;
            if ((pos = path.lastIndexOf("\\")) !== -1) {
                work_dir = path.substring(0, pos);
            }
            var elExec = Components.classes["@ayu.commun.jp/el/exec;1"].createInstance(Components.interfaces.IelExec);
            elExec.exec(path, args, work_dir);
        } 
    },
    browseFile: function(name)
    {
        const filePicker = Components.classes['@mozilla.org/filepicker;1'].createInstance(Components.interfaces.nsIFilePicker);

        filePicker.init(window, name + "の実行ファイルを指定してください。", Components.interfaces.nsIFilePicker.modeOpen);
        filePicker.defaultString = "";
        var res = filePicker.show();
        switch (res) {
            case Components.interfaces.nsIFilePicker.returnOK:
                return filePicker.file.path;
        }
        return false;
    }
};

window.addEventListener('load', elLaunch.init, false);
window.addEventListener('unload', elLaunch.complete, false);
